package com.nagarro.assignment1.validation;

import com.nagarro.assignment1.helper.Constants.TypeOfItem;

/**
 * Class which performs validation checks for the details entered by the user.
 * @author Shreyasi Agrawal
 * @version 1.0
 */
public class Validator {
	
	public boolean validate(String name, String price, String quantity, String type) {
		boolean check = true;
		
		if(!checkPrice(price)) {
			check = false;
		}
		else if(!checkQuantity(quantity)) {
			check = false;
		}
		else if(!checkType(type)) {
			check = false;
		}
		else if(!checkName(name)) {
			check = false;
		}
		return check;
	}
	
	private boolean checkName(String name) {
		boolean valid = true;
		
		// checks if the name is null
		if(name == null) {
			System.out.println("Wrong inputs. Name is empty.");
			valid = false;
		}
		return valid;
	}

	/**
	 * Method to check if type is either raw, manufactured or imported
	 * @param type Parameter to be checked for validity
	 * @return Whether the parameter is valid or not
	 */
	private boolean checkType(String type) {
		boolean valid = true;
		
		if(!(type.equalsIgnoreCase(TypeOfItem.RAW.toString()) || type.equalsIgnoreCase(TypeOfItem.MANUFACTURED.toString()) ||
				type.equalsIgnoreCase(TypeOfItem.IMPORTED.toString()))) {
			System.out.println("Wrong inputs. Type must either be raw, manufactured or imported.");
			valid = false;
		}
		return valid;
	}

	/**
	 * Method to check if the value of quantity is a valid int and > 0
	 * @param quantity Parameter to be checked for validity
	 * @return Whether the parameter is valid or not
	 */
	private boolean checkQuantity(String quantity) {
		boolean valid = true;
		int quant;
		
		// check if quantity is of type int
		try {
			quant = Integer.parseInt(quantity);
		} catch(NumberFormatException e) {
			System.out.println("Wrong input for quantity. It should be a whole number.");
			return false;
		}
		
		if(quant <= 0) {
			System.out.println("Quantity cannot be <= 0.");
			valid = false;
		}
		return valid;
	}

	/**
	 * Method to check if the value of price is a valid double > 0
	 * @param price Parameter to be checked for validity
	 * @return Whether the parameter is valid or not
	 */
	private boolean checkPrice(String price) {
		boolean valid = true;
		double cost;
		
		// check if price is of type double
		try {
			cost = Double.parseDouble(price);
		} catch(NumberFormatException e) {
			System.out.println("Wrong input for price. It should be a real number.");
			return false;
		}
		
		if(cost <= 0) {
			System.out.println("Price cannot be <= 0");
			valid = false;
		}
		return valid;
	}
}